/*
 * =====================================================================================
 *
 *       Filename:  ssb.hpp
 *
 *    Description:  
 *
 *        Version:  0.1a
 *        Created:  17.05.2012 20:04:31
 *       Revision:  none
 *       Compiler:  g++
 *
 *         Author:  Waldemar Gorbunov,
 *   Organization:  
 *
 * =====================================================================================
 */

#ifndef SERVER_HPP_
#define SERVER_HPP_

#define MAXSEND 4096

#include "globals.hpp"
#include "signal.hpp"
#include "ftp.hpp"
#include "client.hpp"

#include <functional>
#include <ev++.h>
#include <unistd.h>
#include <fcntl.h>
#include <memory>
#include <list>

namespace Drosera {

class Server : protected SSLSocket, protected Config {

	public:
		Server ( const char* );
		~Server ( void );

		bool init ( void );
		void stop ( void );
		unsigned int size ( void );
		static void run ( void );
		static void clear ( void );
		static int getId ( SSL*, int& );

		Signal<void,std::shared_ptr<Client>>& signalNewClient ( void );
		Signal<void,std::shared_ptr<Client>>& signalDelClient ( void );

		Signal<void,std::shared_ptr<Ftp>>& signalNewFtp ( void );
		Signal<void,std::shared_ptr<Ftp>, int>& signalDelFtp ( void );

		static void stop ( ev::sig&, int );

	private:
		
		std::vector<std::shared_ptr<Client> > m_client;
		std::vector<std::shared_ptr<Ftp> > m_ftp;
		static ev::default_loop m_loop;
		ev::sig m_sio;
		ev::io m_io;

		Signal<void,std::shared_ptr<Client>> m_signalNewClient;
		Signal<void,std::shared_ptr<Client>> m_signalDelClient;

		Signal<void,std::shared_ptr<Ftp>> m_signalNewFtp;
		Signal<void,std::shared_ptr<Ftp>, int> m_signalDelFtp;

	protected:
		void incomingSocket ( ev::io &watcher, int revents );
		void deleteClient ( unsigned int );
		void deleteFtp ( unsigned int, int );

};

}
#endif
